<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test class of the View class.
     *
     * @package    unittests.kassiop.net
     * @subpackage remoting
     * @author     NairuS
     *
     */
    class ServicesAdminTest extends PHPUnit_Framework_TestCase
    {
        /**
         * Defines the service to test.
         *
         * @var BackService
         */
        public $service ;

        protected function setUp()
        {
            $this->service = new BackService() ;
        }

        protected function tearDown()
        {
            unset( $this->service ) ;
        }

        public function testInterfaces()
        {
            $this->assertTrue( $this->service instanceof ServicesAdmin, 'The class must inherate ServicesAdmin class.' ) ;
            $this->assertTrue( $this->service instanceof ServicesMysql, 'The class must inherate ServicesMysql class.' ) ;
            $this->assertTrue( $this->service instanceof Findable     , 'The class must implement Findable interface.' ) ;
            $this->assertTrue( $this->service instanceof Administrable, 'The class must implement Administrable interface.' ) ;
        }

        public function testAdd()
        {
            $badVo  = array( 'label' => '', 'published' => 'a' );
            $goodVo = array( 'label' => 'a label', 'published' => 1 );
            $this->assertEquals( $this->service->add( $goodVo ), 1, '1.1. This vo must be inserted at id "1"' );
            $this->assertEquals( $this->service->add( $goodVo ), 2, '1.2. This vo must be inserted at id "2"' );
            $this->assertEquals( $this->service->add( $goodVo ), 3, '1.3. This vo must be inserted at id "3"' );

            $result = $this->service->add( $badVo );
            $this->assertTrue( $result instanceof ErrorsModelVO, '2.1 The model must return a ErrorsModelVO');
            $this->assertEquals( count($result->errors), 2, '2.2. There must be 2 errors setted.' );

            $error1 = $result->errors[0];
            $this->assertTrue( $error1 instanceof ErrorVO, '3.1. The first error must be an instance of ErrorVO.');
            $this->assertEquals( $error1->field, 'published', '3.2. The first error must be the published field.' );
            $this->assertEquals( $error1->message, '"a" is not a valid boolean value.', '3.3. The first error message must be correct.' );

            $error2 = $result->errors[1];
            $this->assertTrue( $error2 instanceof ErrorVO, '4.1. The second error must be an instance of ErrorVO.');
            $this->assertEquals( $error2->field, 'label', '4.2. The second error must be the labal field.' );
            $this->assertEquals( $error2->message, 'Required.', '4.2. The second error message must be correct.' );
        }

        public function testEdit()
        {
            $badVo  = array( 'id' => 1, 'label' => '', 'published' => 'a' );
            $goodVo = array( 'id' => 1, 'label' => 'a label changed', 'published' => 0 );

            $this->assertTrue( $this->service->edit( $goodVo ), '1.1. The update query has to be excecuted successfully.');

            $result = $this->service->edit( $badVo );
            $this->assertTrue( $result instanceof ErrorsModelVO, '2.1. The model must return a ErrorsModelVO');
            $this->assertEquals( count($result->errors), 2, '2.2. There must be 2 errors setted.' );

            $error1 = $result->errors[0];
            $this->assertTrue( $error1 instanceof ErrorVO, '3.1. The first error must be an instance of ErrorVO.');
            $this->assertEquals( $error1->field, 'published', '3.2. The first error must be the published field.' );
            $this->assertEquals( $error1->message, '"a" is not a valid boolean value.', '3.3. The first error message must be correct.' );

            $error2 = $result->errors[1];
            $this->assertTrue( $error2 instanceof ErrorVO, '4.1. The second error must be an instance of ErrorVO.');
            $this->assertEquals( $error2->field, 'label', '4.2. The second error must be the labal field.' );
            $this->assertEquals( $error2->message, 'Required.', '4.2. The second error message must be correct.' );
        }

        public function testDelete()
        {
            $this->assertTrue( $this->service->delete(1), 'The delete query has to be executed successfully.');
        }


        public function testFind()
        {
            $result = $this->service->find(2);
            $this->assertTrue($result instanceof BackVO, '1 The result must be an instance of BackVO.');
        }

        public function testFindAll()
        {
            $result = $this->service->findAll();
            $this->assertTrue( count( $result ) > 0, '1 The query must return more than 0 records.');
            $this->assertTrue( count( $result ) == 2, '2 The query must return 2 records.');
        }

        public function testTruncate()
        {
            $this->assertTrue( $this->service->truncate(), 'The truncate query has to be executed successfully.' );
        }
    }
?>